﻿
/**
 * linkbutton - jQuery xui
 *
 * Licensed under the GPL:
 *   http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2013 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {

    function buildButton(target) {
        var opts = $.data(target, 'linkbutton').options;
        var t = $(target).empty().addClass('x-btn');
        t.attr('id', opts.id || '');
        
        if (opts.plain) {
            t.addClass('x-btn-plain')
        }
        
        var inner = $('<span class="x-btn-text">&nbsp;</span>');
        if (opts.text) {
            inner.html(opts.text).appendTo(t);
        } else {
            inner.addClass('x-btn-empty').appendTo(t);
        }
        if (opts.iconCls) {
            inner.addClass(opts.iconCls).addClass('x-btn-icon-' + opts.iconAlign);
        }

        setDisabled(target, opts.disabled);
    }

    function setDisabled(target, disabled) {
        var state = $.data(target, 'linkbutton');
        var opts = state.options;
        $(target).removeClass('x-btn-disabled');
        if (disabled) {
            opts.disabled = true;
            var href = $(target).attr('href');
            if (href) {
                state.href = href;
                $(target).attr('href', 'javascript:void(0)');
            }
            if (target.onclick) {
                state.onclick = target.onclick;
                target.onclick = null;
            }
        } else {
            opts.disabled = false;
            if (state.href) {
                $(target).attr('href', state.href);
            }
            if (state.onclick) {
                target.onclick = state.onclick;
            }
        }
    }

    $.fn.linkbutton = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.linkbutton.methods[options](this, param);
        }

        options = options || {};
        return this.each(function() {
            var state = $.data(this, 'linkbutton');
            if (state) {
                $.extend(state.options, options);
            } else {
                $.data(this, 'linkbutton', {
                    options: $.extend({}, $.fn.linkbutton.defaults, $.fn.linkbutton.parseOptions(this), options)
                });
            }
            buildButton(this);
        });
    };

    $.fn.linkbutton.methods = {
        options: function(jq) {
            return $.data(jq[0], 'linkbutton').options;
        },
        enable: function(jq) {
            return jq.each(function() {
                setDisabled(this, false);
            });
        },
        disable: function(jq) {
            return jq.each(function() {
                setDisabled(this, true);
            });
        }
    };

    $.fn.linkbutton.parseOptions = function(target) {
        var t = $(target);
        return $.extend({}, $.parser.parseOptions(target, ['id']), {
            text: $.trim(t.html())
        });
    };

    $.fn.linkbutton.defaults = {
        id: null,
        disabled: false,
        plain: false,
        text: '',
        iconCls: null,
        iconAlign: 'left'
    };

})(jQuery);
